(ns samber.services.question.question
  (:require
    [toucan.db :as db]
    [samber.db.models :as models]
    [samber.middleware.exception :as exception]
    [clojure.tools.logging :as log]))


(defn create [model uid]
  (let [entity (first (db/select models/User :id uid))]
    (if (empty? entity)
      (throw (ex-info "check" {:type ::exception/check :msg "user not exists!"})))

    ;; check locked
    (if (:locked_at entity)
      (throw (ex-info "check" {:type ::exception/check :msg "locked!"}))))

  (if (empty? (:content model))
    (throw (ex-info "check" {:type ::exception/check :msg "content can't be empty!"})))

  (db/insert! models/Questions (assoc model :uid uid)))

(defn fetch [id]
  (let [entity (db/select-one models/Questions :id id)]
    (if (empty? entity)
      (throw (ex-info "check" {:type ::exception/not-found :msg "question not exists!"})))

    entity))
